import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import HomeView from '../views/user/HomeView.vue'
import EventsView from '../views/user/event/EventsView.vue'
import KnowledgeView from '../views/user/knowledge/KnowledgeView.vue'
import CoursesView from '../views/user/course/CoursesView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/',
      component: () => import('../views/admin/Layout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          name: 'home',
          component: HomeView,
        },
        {
          path: 'profile',
          name: 'profile',
          component: () => import('../views/ProfileView.vue'),
        },
        {
          path: 'consultation',
          name: 'consultation',
          component: () => import('../views/user/consultation/ConsultationView.vue'),
        },
        {
          path: 'events',
          name: 'events',
          component: EventsView,
        },
        {
          path: 'courses',
          name: 'courses',
          component: CoursesView,
        },
        {
          path: 'knowledge',
          name: 'knowledge',
          component: KnowledgeView,
        },
      ],
    },
    {
      path: '/staff',
      component: () => import('../views/admin/Layout.vue'),
      meta: { requiresAuth: true, requiresStaff: true },
      children: [
        {
          path: '',
          name: 'staff-home',
          component: () => import('../views/admin/DashboardView.vue'),
        },
      ],
    },
    {
      path: '/course/:id',
      name: 'CourseDetail',
      component: () => import('@/views/user/course/CourseDetail.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/event/:id',
      name: 'EventDetail',
      component: () => import('@/views/user/event/EventDetail.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/knowledge/:id',
      name: 'KnowledgeDetail',
      component: () => import('@/views/user/knowledge/KnowledgeDetail.vue'),
      meta: { requiresAuth: true },
    },
   
  ],
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    // 未登录，跳转到登录页
    next('/login')
  } else if (to.meta.requiresStaff && !(userStore.isAdmin || userStore.isNurse)) {
    // 非工作人员访问工作人员页面，跳转到首页
    next('/')
  } else if (userStore.isLoggedIn && (to.path === '/login' || to.path === '/register')) {
    // 已登录用户访问登录/注册页，重定向到对应页面
    next(userStore.isAdmin || userStore.isNurse ? '/staff' : '/')
  } else {
    next()
  }
})

export default router
